<?php
/**
 * Created by PhpStorm.
 * User: 张鹏
 * Initial-Time: 2017-06-01 下午 3:25
 * Versions：v1.00
 * Last-time: 2017-06-01 下午 3:25
 * Desc：文件描述，修改记录
 */

namespace app\hongyanche\model;


class Upload
{
    protected $fileName;//文件名
    protected $maxSize;// 最大上传文件
    protected $allowExt;// 上传文件的扩展名
    protected $allowMime;// 上传文件的类型
    protected $uploadPath;// 上传文件的路径
    protected $imgFlag;//是否验证上传图片的类型
    protected $fileInfo;//上传文件的信息
    protected $error;//错误提示信息
    protected $ext;//文件的后缀名
    protected $destination;//存放文件的具体路径
    protected $unName; // 保存的文件名
    public function __construct($fileName=0,$imgFlag=true,$uploadPath='upload',$maxSize=5242880000,$allowExt=array('mp4','wmv','flv','avi'),$allowMime=array('video/mp4','video/x-ms-wmv','video/avi','image/gif')){
        $this -> fileName = $fileName;
        $this -> maxSize = $maxSize;
        $this -> allowMime = $allowMime;
        $this -> allowExt = $allowExt;
        $this -> uploadPath = $uploadPath;
        $this -> imgFlag = $imgFlag;
        
    }

    // 检测上传文件是否出错
    protected function checkError(){
        if($this -> fileInfo['error']>0){
            switch($this -> fileInfo['error']){
                case 1:
                    $this -> error = ['state'=>0,'errormsg'=>'上传文件超过了php.ini 中 upload_max_filesize 选项限制的值'];
                    break;
                case 2:
                    $this -> error = ['state'=>0,'errormsg'=>'上传文件的大小超过了HTML表单中 MAX_FILE_SIZE 选项指定的值'];
                    break;
                case 3:
                    $this -> error = ['state'=>0,'errormsg'=>'文件只有部分上传'];
                    break;
                case 4:
                    $this -> error = ['state'=>0,'errormsg'=>'没有选择上传的文件'];
                    break;
                case 6:
                    $this -> error = ['state'=>0,'errormsg'=>'找不到临时目录'];
                    break;
                case 7:
                    $this -> error = ['state'=>0,'errormsg'=>'文件上传错误'];
                    break;
                case 8:
                    $this -> error = ['state'=>0,'errormsg'=>'上传文件被php扩展中断'];
                    break;
            }
            return false;
        }else{
            return true;
        }

    }

    // 判断文件的大小
    protected function checkSize(){
        if($this -> fileInfo['size']>$this -> maxSize){
            $this -> error = ['state'=>0,'errormsg'=>'上传文件超过的最大上传文件限制'];
            return false;
        }
        return true;
    }

    // 判断文件扩展名
    protected function checkExt(){
        $this -> ext = pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION);
        if(!in_array($this->ext,$this->allowExt)){
            $this -> error = ['state'=>0,'errormsg'=>'上传文件的格式不正确'];
            return false;
        }
        return true;
    }

    // 检测文件类型
    protected function checkMime(){
        if(!in_array($this->fileInfo['type'],$this->allowMime)){
            $this -> error = ['state'=>0,'errormsg'=>'上传文件类型不正确'];
            return false;
        }
        return true;
    }

    // 检测是否为真实图片
//    protected function checkImgTrue(){
//        if(!empty($this->imgFlag)){
//            if(!@getimagesize($this->fileInfo['tmp_name'])){
//                $this -> error = ['state'=>0,'errormsg'=>'上传文件不是真实的图片'];
//                return false;
//            }
//            return true;
//        }
//    }
//
//    // 判断文件是否是通过http post方式上传上来的
//    protected function checkHTTPPOST(){
//        if(!is_uploaded_file($this->fileInfo['tmp_name'])){
//            $this -> error = ['state'=>0,'errormsg'=>'文件不是通过http post方式上传上来的'];
//            return false;
//        }
//        return true;
//    }

    // 显示错误信息
//    protected function showError(){
//        // return $this->error;
//        <sapn style='color:red;'>".$this->error."</sp;
//    }

    // 检测目录书否存在
    protected function checkPath(){
        if(!file_exists($this->uploadPath)){
            mkdir($this->uploadPath,0777,true);
        }
    }

    // 生成一个随机的字符串
    protected function getunName(){
        return md5(uniqid(microtime(true),true));
    }

    //上传文件
    public function uploadFiles(){
        $this->fileInfo = $_FILES[0];
        if($this->checkError()&&$this->checkSize()&&$this->checkExt()){
            $this -> checkPath();
            $this -> unName = $this -> getunName();
            $this -> destination = $this -> uploadPath.'/'.$this-> unName.'.'.$this -> ext;
            if(move_uploaded_file($this->fileInfo['tmp_name'],$this -> destination)){
                return ['state'=>1,'data'=>$this -> destination];
            }else{
                return $this->error;
//                $this -> showError();
            }
        }else{
            return $this->error;
        }
    }
}